package nl.ejvos.bgpgames.cbbchess.client;

import nl.ejvos.bgpgames.cbbchess.shared.MoveMessage;
import nl.ejvos.bgpgames.cbbchess.shared.OpponentMovedMessage;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.hu.bgp.framework.clientapi.Client;
import com.hu.bgp.framework.clientapi.ServerToClientMessage;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ChessClient extends Client
{
	private final VerticalPanel panel = new VerticalPanel();
	private Board board;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad2()
	{
		
		
		board = new Board(panel.getElement(), this);
	}

	/**
	 * GWT JSNI voorbeeld (niet gebruikt)
	 */
	private static native void createBoards() /*-{
												var allDivs = $doc.getElementsByTagName("div");
												for (var i = 0; i < allDivs.length; i++) {
												if (allDivs[i].className == "board") {
												//@nl.ejvos.bgpgames.cbbchess.client.Board::Create(Lcom/google/gwt/user/client/Element;)(allDivs[i]);
												}
												}
												}-*/;

	protected Widget getPlayArea()
	{
		return panel;
	}

	protected void handleMessage(ServerToClientMessage message)
	{
		if (message instanceof OpponentMovedMessage)
		{
			Window.alert("omm");
			OpponentMovedMessage omm = (OpponentMovedMessage) message;
			Square toSquare = board.m_squares[omm.getToX()][omm.getToY()];
			board.m_squares[omm.getX()][omm.getY()].getPiece().move(toSquare);
		}
	}

	public void onFailure(Throwable caught)
	{
		Window.alert(caught.getMessage());
	}

	public void broadcastMoveMessage(int x, int y, int toX, int toY)
	{
		MoveMessage message = new MoveMessage(x, y, toX, toY);
		sendMessageAndIgnoreOutcome(message);
	}
}
